package org.bee.service;

import org.bee.enums.IsDelete;
import org.bee.input.BatchJobSearchVo;
import org.bee.mappers.BatchJobMapper;
import org.bee.model.BatchJob;
import org.bee.model.BatchJobExample;
import org.bee.string.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 陈国松
 * @date 2025-01-10 11:06:36
 * @description 定时任务调度表Service层
 */
@Service
public class BatchJobService{

    private final BatchJobMapper batchJobMapper;

    public BatchJobService(BatchJobMapper batchJobMapper) {
        this.batchJobMapper = batchJobMapper;
    }

    /**
     * 插入定时任务调度表
     * @param batchJob 定时任务调度表实体
     * @return 插入结果
     */
    public int insert(BatchJob batchJob) {
        return batchJobMapper.insert(batchJob);
    }

    /**
     * 删除定时任务调度表
     * @param batchJobId 定时任务调度表id
     * @return 删除结果
     */
    public int delete(String batchJobId,Boolean isDelete) {
        if(isDelete){
            return batchJobMapper.deleteByPrimaryKey(batchJobId);
        }else{
            BatchJob batchJob = new BatchJob();
            batchJob.setId(batchJobId);
            batchJob.setIsDeleted(true);
            return batchJobMapper.updateByPrimaryKeySelective(batchJob);
        }
    }

    /**
     * 根据主键更新定时任务调度表
     * @param batchJob 定时任务调度表实体
     * @return 更新结果
     */
    public int update(BatchJob batchJob) {
        return batchJobMapper.updateByPrimaryKeySelective(batchJob);
    }

    /**
     * 查询定时任务调度表
     * @param batchJobSearchVo 定时任务调度表查询参数
     * @return 定时任务调度表列表
     */
    public List<BatchJob> select(BatchJobSearchVo batchJobSearchVo) {
        BatchJobExample batchJobExample = new BatchJobExample();
        BatchJobExample.Criteria criteria = batchJobExample.createCriteria();
        if (StringUtils.isNotEmpty(batchJobSearchVo.getName())) {
            criteria.andNameLike("%"+batchJobSearchVo.getName()+"%");
        }
        if (StringUtils.isNotEmpty(batchJobSearchVo.getJobGroup())) {
            criteria.andJobGroupEqualTo(batchJobSearchVo.getJobGroup());
        }
        if (StringUtils.isNotEmpty(batchJobSearchVo.getBean())) {
            criteria.andBeanLike("%"+batchJobSearchVo.getBean()+"%");
        }
        if (null!=batchJobSearchVo.getStatus()) {
            criteria.andStatusEqualTo(batchJobSearchVo.getStatus());
        }
        criteria.andIsDeletedEqualTo(Boolean.valueOf(IsDelete.FALSE.getCode()));
        return batchJobMapper.selectByExample(batchJobExample);
    }

    /**
     * 根据主键查询定时任务调度表
     * @param id 定时任务调度表id
     * @return 定时任务调度表实体
     */
    public BatchJob selectById(String id) {
        return batchJobMapper.selectByPrimaryKey(id);
    }
}